package com.etms.service;

import com.etms.mapper.AttendAdminMapper;
import com.etms.mapper.AttendMapper;
import com.etms.pojo.AccountAdmin;
import com.etms.pojo.AccountMsg;
import com.etms.pojo.Attend;
import com.etms.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class ThreadService {

    @Async("taskExecutor")
    public void updateAttend(AttendMapper attendMapper,Integer uid){//异步更新工作量
        Attend isOk = attendMapper.findByDayAndUid(Calendar.getInstance().get(Calendar.DAY_OF_YEAR), uid);//查看是否已经签到
        if(isOk!=null){//不签到,作为惩罚,不增加工作量
            Integer workNums = isOk.getWorkNums();
            Integer id = isOk.getId();
            attendMapper.updateWorks(workNums,workNums+1,id);//乐观锁更新工作量
            try {
                TimeUnit.SECONDS.sleep(2);//睡眠3秒
                System.out.println("工作量更新..");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public void insertMsg(AccountsService accountsService, String msg) {
        AccountMsg accountMsg=new AccountMsg();
        accountMsg.setMsg(msg).setAcId(1).setCreateTime(DateUtils.getDateTime(new Date()));
        accountsService.insertMsg(accountMsg);//增加账户操作日志
            System.out.println("账户日志更新..");

    }


    public void insertAD(AttendAdminMapper attendAdminMapper, AccountAdmin accountAdmin) {
        attendAdminMapper.addOne(accountAdmin);//增加账户管理信息
            System.out.println("账户管理信息更新..");
    }
}
